package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.DepMapper;
import cn.itcast.erp.dao.mapper.EmpMapper;
import cn.itcast.erp.dao.mapper.GoodstypeMapper;
import cn.itcast.erp.entity.Dep;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Goodstype;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.exception.SameUsernameException;
import cn.itcast.erp.params.EmpParam;
import cn.itcast.erp.service.EmpService;
import jdk.nashorn.internal.codegen.CompilerConstants;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.List;

@Service("empService")
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpMapper empMapper;
    @Resource
    private DepMapper depMapper;



    //新增员工
    @Override
    public int insertEmp(Emp emp) throws SameUsernameException {

        //数据校验:username不能重复
        Emp username = this.empMapper.searchByUsername (emp.getUsername ( ));
        if (username!=null){
            throw new SameUsernameException ("用户名已存在111");
        }
        //初始密码默认=username
        emp.setPwd (emp.getUsername ());
        return this.empMapper.insertEmp (emp);
    }

    //删除员工
    @Override
    public int deleteEmp(Long uuid) {
        return this.empMapper.deleteEmp (uuid);
    }

    //更新员工信息
    @Override
    public int updateEmp(Emp emp) {
        return this.empMapper.updateEmp (emp);}

    //查询员工信息
    @Override
    public AjaxResult searchEmp(Emp param) {
        AjaxResult ar = new AjaxResult ( );
        int total = this.empMapper.count (param);
        List<Emp> empList = this.empMapper.searchEmp (param);
        ar.setTotal (total);
        ar.setRows (empList);
        return ar;
    }

    //查询所有员工（不分页）
    @Override
    public List<Emp> findAllEmp() {
        return empMapper.findAllEmp();
    }

    //查询员工by uuid
    @Override
    public List<Emp> searchEmpAll(Emp param) {
        return empMapper.searchEmpAll (param);
    }

    @Override
    public Emp searchEmpById(Long uuid) {
        return this.empMapper.searchEmpById (uuid);
    }

    //修改密码
    @Override
    public  AjaxResult updatePwd(Long uuid, String oldPwd, String newPwd) {
        Emp emp = this.empMapper.selectEmpPassword(uuid);

        AjaxResult ar = new AjaxResult ( );

        if (emp.getPwd().equals(oldPwd)){
            String id = String.valueOf(uuid);
            int i = this.empMapper.updatePwd(id, newPwd);
            if (i>0){
                ar.setMsg("修改密码成功");
            }else{
                ar.setMsg("修改密码失败");
            }

        }else {
            ar.setMsg("原密码输入错误");
        }
        return ar;
    }


    @Override
    public int updatePwd(String id, String newPwd) {
        return empMapper.updatePwd(id, newPwd);
    }

    @Override
    public Emp findByUsernameAndPwd(Emp emp) {
        return empMapper.findEmp(emp);
    }

    //导出Excel
    @Override
    public InputStream getAllEmpEx() {
        //添加数据
        List<Emp> emp = empMapper.getAllEmpEx();

        HSSFWorkbook sheets = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = sheets.createSheet("员工信息表");  // 创建第一个新的Sheet页

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");




        //创建第一行（也可以成为表头）
        Row row1 = sheet.createRow(0);
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 30 * 256);
        sheet.setColumnWidth(2, 30 * 256);
        sheet.setColumnWidth(3, 20 * 256);
        sheet.setColumnWidth(4, 30 * 256);
        sheet.setColumnWidth(5, 30 * 256);
        sheet.setColumnWidth(6, 20 * 256);
        sheet.setColumnWidth(7, 30 * 256);
        sheet.setColumnWidth(8, 30 * 256);


        //给表头第一行一次创建单元格
        String[] headers = {"编号","登陆名","真实姓名","性别","邮件地址","联系电话","联系地址","出生年月","部门名称"};
        for(int i = 0; i < headers.length; i++){
            row1.createCell(i).setCellValue(headers[i]);
            row1.setHeightInPoints(30);//设置行的高度是30
        }


        //向单元格里添加数据
        for(short i=0;i<emp.size();i++) {
            row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(emp.get(i).getUuid());
            row1.createCell(1).setCellValue(emp.get(i).getUsername());
            row1.createCell(2).setCellValue(emp.get(i).getName());
            if (emp.get(i).getGender()==0){
                row1.createCell(3).setCellValue("女");
            }else  if (emp.get(i).getGender()==1){
                row1.createCell(3).setCellValue("男");
            }
            row1.createCell(4).setCellValue(emp.get(i).getEmail());
            row1.createCell(5).setCellValue(emp.get(i).getTele());
            row1.createCell(6).setCellValue(emp.get(i).getAddress());
            row1.createCell(7).setCellValue(sdf.format(emp.get(i).getBirthday()));
            //通过部门的uuid来查询部门的名字返回给员工
            Dep byuuid = depMapper.findByuuid(emp.get(i).getDepuuid());
            row1.createCell(8).setCellValue(byuuid.getName());
            row1.setHeightInPoints(30);//设置行的高度是30
        }
        //字节流缓存区
        ByteArrayOutputStream bo = new ByteArrayOutputStream();


        try {
            sheets.write(bo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ByteArrayInputStream(bo.toByteArray());
    }


}
